{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "understanding-prospect",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I'm an instance of class B\n",
      "I'M AN INSTANCE OF CLASS B\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "defined \u001b[32mclass\u001b[39m \u001b[36mA\u001b[39m\r\n",
       "defined \u001b[32mclass\u001b[39m \u001b[36mB\u001b[39m\r\n",
       "defined \u001b[32mtrait\u001b[39m \u001b[36mC\u001b[39m\r\n",
       "defined \u001b[32mclass\u001b[39m \u001b[36mD\u001b[39m\r\n",
       "\u001b[36md\u001b[39m: \u001b[32mD\u001b[39m = ammonite.$sess.cmd22$Helper$D@7ae6eaa5"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "abstract class A {\n",
    "  val message: String\n",
    "}\n",
    "class B extends A {\n",
    "  val message = \"I'm an instance of class B\"\n",
    "}\n",
    "trait C extends A {\n",
    "  def loudMessage = message.toUpperCase()\n",
    "}\n",
    "class D extends B with C\n",
    "\n",
    "val d = new D\n",
    "println(d.message)  // I'm an instance of class B\n",
    "println(d.loudMessage)  // I'M AN INSTANCE OF CLASS B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "loose-psychiatry",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "defined \u001b[32mclass\u001b[39m \u001b[36mAbs\u001b[39m"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "abstract class Abs{\n",
    "    type T\n",
    "    def hasNext:Boolean\n",
    "    def next():T\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "published-default",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "defined \u001b[32mclass\u001b[39m \u001b[36mStringIterator\u001b[39m"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "class StringIterator(s:String) extends Abs {\n",
    "    type T = Char\n",
    "    private var i = 0\n",
    "    def hasNext = i < s.length\n",
    "    def next ={\n",
    "        val ch = s charAt i\n",
    "        i += 1\n",
    "        ch\n",
    "    }\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "agreed-irish",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "defined \u001b[32mtrait\u001b[39m \u001b[36mRichIterator\u001b[39m"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "trait RichIterator extends Abs {\n",
    "    def foreach1(f: T=>Unit):Unit = while(hasNext) f(next())\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "central-salvation",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "defined \u001b[32mclass\u001b[39m \u001b[36mRichStringIter\u001b[39m\r\n",
       "\u001b[36mrichStringIter\u001b[39m: \u001b[32mRichStringIter\u001b[39m = ammonite.$sess.cmd30$Helper$RichStringIter@40c09aed"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "class RichStringIter extends StringIterator(\"Scala\") with RichIterator\n",
    "val richStringIter = new RichStringIter\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "animal-financing",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "S\n"
     ]
    }
   ],
   "source": [
    "println(richStringIter.next )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "compact-acting",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "c\n"
     ]
    }
   ],
   "source": [
    "println(richStringIter.next )\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "rough-arcade",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a\n",
      "l\n",
      "a\n"
     ]
    }
   ],
   "source": [
    "richStringIter foreach1 println"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "amateur-azerbaijan",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Scala",
   "language": "scala",
   "name": "scala"
  },
  "language_info": {
   "codemirror_mode": "text/x-scala",
   "file_extension": ".scala",
   "mimetype": "text/x-scala",
   "name": "scala",
   "nbconvert_exporter": "script",
   "version": "2.11.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
